package api

import (
	"errors"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/smash-llj/Nexa-server/models/request"
	"github.com/smash-llj/Nexa-server/models/response"
	service "github.com/smash-llj/Nexa-server/service"
	utils "github.com/smash-llj/Nexa-server/utils"
)

type GroupApi struct {
	BaseApi
	Service *service.GroupService
}

func NewGroupApi() GroupApi {
	return GroupApi{
		BaseApi: NewBaseApi(),
		Service: service.NewGroupService(),
	}
}

func (g *GroupApi) CreateGroup(C *gin.Context) {
	g.BaseApi.C = C
	UserId := utils.GetUserId(C)
	var member []int
	err := C.ShouldBind(&member)
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
	}
	result, err := g.Service.CreateGroup(UserId, member)
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	g.Success(Response{Msg: "创建成功", Data: result})
}

// GetGroupMessage 获取群聊消息
func (g *GroupApi) GetGroupMessage(C *gin.Context) {
	var req request.GetGroupMessageReq
	userId := utils.GetUserId(C)
	if err := g.BindData(BuildBaseApiOptions{C: C, Dto: &req}); err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	result, err := g.Service.GetGroupMessage(req, userId)
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
	}
	C.JSON(http.StatusOK, gin.H{
		"status": 200,
		"data":   result,
	})
}

func (g *GroupApi) DetailGroup(C *gin.Context) {
	g.BaseApi.C = C
	groupId, err := strconv.ParseUint(C.Param("groupId"), 10, 32)
	if err != nil {
		g.Fail(Response{Msg: errors.New("groupId不能为空")})
		return
	}
	data, list, err := g.Service.DetailGroup(uint(groupId))
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
	}
	result := &response.GroupInfoResp{
		Info:   data,
		Member: list,
	}
	g.Success(Response{Data: result})
}

// GetUserGroups 查询已经加入的群聊
func (g *GroupApi) GetUserGroups(C *gin.Context) {
	g.BaseApi.C = C
	uId := utils.GetUserId(C)
	list, err := g.Service.GetUserGroups(uId)
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	g.Success(Response{Data: list})
}

func (g *GroupApi) GetGroupMemberList(C *gin.Context) {
	g.BaseApi.C = C
	groupId, err := strconv.ParseUint(C.Param("groupId"), 10, 32)
	if err != nil {
		g.Fail(Response{Msg: errors.New("groupId不能为空")})
		return
	}
	info, err := g.Service.GetGroupMemberList(uint(groupId))
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
	}
	g.Success(Response{Data: info})
}

func (g *GroupApi) JoinGroup(C *gin.Context) {
	var groupParams request.JoinGroupParamsReq
	if err := g.BindData(BuildBaseApiOptions{C: C, Dto: &groupParams}); err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	if err := g.Service.JoinGroup(groupParams); err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	g.Success(Response{Msg: "ok"})
}

func (g *GroupApi) UpdateGroupInfo(C *gin.Context) {
	var groupParams request.UpdateGroupInfoReq
	if err := g.BindData(BuildBaseApiOptions{C: C, Dto: &groupParams}); err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	err := g.Service.UpdateGroupInfo(groupParams)
	if err != nil {
		g.Fail(Response{Msg: err.Error()})
		return
	}
	g.Success(Response{Msg: "ok"})
}
